<template>
  <view class="home-container">
    <!-- uniapp的指令，要使用vue的 -->
    <swiper autoplay circular indicator-dots indicator-active-color="#fff">
      <swiper-item v-for="item in swipers" :key="item.bannerid">
        <image :src="item.img" />
      </swiper-item>
    </swiper>
    <!-- 菜单数据 -->
    <uni-grid :column="5" :showBorder="false">
      <uni-grid-item v-for="item in menus" :key="item.id">
        <view class="grid-item-box">
          <image :src="item.icon" />
          <text>{{ item.title }}</text>
        </view>
      </uni-grid-item>
    </uni-grid>
    <!-- 商品列表 -->
    <view class="goods-list">
      <view
        v-for="item in goodsList"
        :key="item.proid"
        class="goods-item"
        @click="goToDetail(item.proid)"
      >
        <!-- <navigator
        :url="`/subpkg/goods-detail/index?proid=${item.proid}`"
        v-for="item in goodsList"
        :key="item.proid"
        class="goods-item"
      > -->
        <image :src="item.img1" />
        <text class="title">{{ item.proname }}</text>
        <text class="desc">{{ item.desc }}</text>
        <text class="price">￥{{ formatPrice(item.originprice) }}</text>
        <!-- </navigator> -->
      </view>
    </view>
    <!-- 底部提示 -->
    <view class="footer">
      <van-loading size="20px" type="spinner" v-if="hasMore"
        >拼命加载中...</van-loading
      >
      <text v-else>数据到底了</text>
    </view>
  </view>
</template>

<script setup lang="ts">
import { onReachBottom, onPullDownRefresh } from '@dcloudio/uni-app'
import { ref, watch } from 'vue'
import type { Swiper, Menu, Goods } from '@/types/home'
import { getSwipers, getMenus, getGoodsList } from '@/api/home'
import { useTabBarBadge, useTabBarTitle } from '@/hooks'
useTabBarBadge()
useTabBarTitle(3, '我的', '未登录')
// import { onShow } from '@dcloudio/uni-app'
// import { useUserStore } from '@/store/user'
// const store = useUserStore()
// import { useCartStore } from '@/store/cart'
// const store = useCartStore()
// 定义模型数据
const swipers = ref<Swiper[]>([])
const menus = ref<Menu[]>([])
const page = ref(1)
const hasMore = ref(true)
const goodsList = ref<Goods[]>([])

const getSwipersData = async () => {
  const res = await getSwipers()

  swipers.value = res.data
}

const getMenusData = async () => {
  const res = await getMenus()

  menus.value = res.data
}

const getGoodsListData = async () => {
  const res = await getGoodsList(page.value)

  if (res.data.length > 0) {
    goodsList.value = [...goodsList.value, ...res.data]
  } else {
    hasMore.value = false
  }
}

const formatPrice = (price: number) => {
  return price.toFixed(2)
}

// 上拉加载更多
onReachBottom(() => {
  if (!hasMore.value) return
  page.value++
  getGoodsListData()
})

// 下拉刷新
onPullDownRefresh(() => {
  page.value = 1
  goodsList.value = []
  hasMore.value = true

  getGoodsListData()
})

getSwipersData()
getMenusData()
getGoodsListData()

// 跳转到详情页面
const goToDetail = (proid: string) => {
  uni.navigateTo({
    url: '/subpkg/goods-detail/index?proid=' + proid
  })
}

// // 跟徽标相关
// onShow(() => {
//   if (store.getTotal > 0) {
//     uni.setTabBarBadge({
//       index: 2,
//       text: store.getTotal + ''
//     })
//   } else {
//     uni.removeTabBarBadge({
//       index: 2
//     })
//   }
// })

// watch(
//   () => store.getTotal,
//   newValue => {
//     if (newValue > 0) {
//       uni.setTabBarBadge({
//         index: 2,
//         text: newValue + ''
//       })
//     } else {
//       uni.removeTabBarBadge({
//         index: 2
//       })
//     }
//   },
//   {
//     immediate: true
//   }
// )

// 根据是否登录来设置tabBar第四个的文字
// onShow(() => {
//   if (store.user.token) {
//     uni.setTabBarItem({
//       index: 3,
//       text: '我的'
//     })
//   } else {
//     uni.setTabBarItem({
//       index: 3,
//       text: '未登录'
//     })
//   }
// })
</script>

<style lang="less" scoped>
.home-container {
  swiper {
    width: 750rpx;
    height: 480rpx;

    image {
      width: 100%;
      height: 100%;
    }
  }
  .grid-item-box {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    image {
      margin-top: 20rpx;
      width: 70rpx;
      height: 70rpx;
    }
    text {
      margin-top: 18rpx;
      color: #646566;
      font-size: 28rpx;
      word-break: break-all;
    }
  }
  .goods-list {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    box-sizing: border-box;
    width: 100%;
  }

  .goods-item {
    box-sizing: border-box;
    background-color: #fff;
    padding: 5.33333vw;
    border-radius: 1.33333vw;
    display: flex;
    align-items: center;
    width: 48%;
    flex-direction: column;
    margin-top: 1.33333vw;
  }

  .goods-item image {
    width: 26.66667vw;
    height: 32vw;
  }

  .title {
    font-size: 3.73333vw;
    display: inline-block;
    white-space: nowrap;
    width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 4vw;
    color: #000;
  }

  .desc {
    font-size: 3.2vw;
    color: #c3c3c3;
    display: inline-block;
    white-space: nowrap;
    width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 2.66667vw;
  }

  .price {
    font-size: 3.2vw;
    margin-top: 2.66667vw;
    color: red;
  }

  .footer {
    text-align: center;
    height: 100rpx;
    line-height: 100rpx;
    color: gray;
    font-size: 14px;
  }
}
</style>
